<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <script src="js/jquery.min.js"></script>
    <style>
        .login-dialog {
            width: 400px;
            height: 550px;
            background-color: rgba(255, 255, 255, 0.8);
            border-radius: 10px;
        }
        .login-dialog input {
            font-size: 10px;
        }
    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.png" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="login.html">登陆</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="login-container">
        <!-- 中间的注册框 -->
        <div class="login-dialog">
            <h1>博客系统用户注册</h1>
            <div class="app-header">
                <img src="img/yonghu.png" alt="">
                <span>代码改变世界</span>
            </div>
            
            <div class="row">
                <span>昵称</span>
                <input type="text" id="nickname">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <span>确认密码</span>
                <input type="password" id="password2">
            </div>
            <div class="row">
                <span>邮箱(用户名)</span>
                <input type="text" id="email">
            </div>
            <div class="row">
                <span>git地址</span>
                <input placeholder="(选填)" type="text" id="git">
            </div>
            
            <div class="row">
                <button id="submit" onclick="mysub()">提交</button>
            </div>
        </div>
    </div>
</body>
<script>
    function isValidEmail(email) {
        var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        return regex.test(email);
    }

    function mysub() {
        //1.非空校验
        var nickname = jQuery("#nickname");
        var password = jQuery("#password");
        var password2 = jQuery("#password2");
        var email  = jQuery('#email');
        var gitUrl = jQuery('#git')
        if(nickname.val() == "") {
            alert("请先输入昵称！")
            username.focus();
            return false;
        }
        if(password.val() == "") {
            alert("请先输入密码！")
            password.focus();
            return false;
        }
        if(password2.val() == "") {
            alert("请先输入密码！")
            password2.focus();
            return false;
        }
        if(password.val()!=password2.val()) {
            alert("两次密码输入不一致，请先检查！")
            password.focus();
            return false;
        }
        if(email.val() == "") {
            alert("请先输入邮箱！")
            return false;
        } else {
            if (!isValidEmail(email.val())) {
                alert("输入非法的邮箱格式，请检查");
                return false;
            } 
        }
        //发送ajax给后端
        jQuery.ajax({
            url:"/user/reg",
            type:"POST",
            data:{
                "nickname": nickname.val(),
                "username": email.val(),
                "password": password.val(),
                "gitUrl": gitUrl.val()
            },
            success:function(body) {
                if(body.code==200 && body.data==1) {
                    alert("恭喜，注册成功！");
                    if(confirm("是否现在登录？")) {
                        location.href = "login.html";
                    } 
                } else {
                    alert("抱歉，请重新再试！");
                }
            }
        })
    }
</script>
</html>